package com.diapoclic.entitymanager;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.Cookie;

import com.diapoclic.entitybean.Document;
import com.diapoclic.entitybean.Folder;
import com.diapoclic.entitydao.DocumentDao;
import com.diapoclic.entitydao.FolderDao;

public class FolderManager {

	
	private FolderDao folderDao=new FolderDao();
	private DocumentDao documentDao=new DocumentDao();

	private Folder folderConnect=new Folder();

	
	public void helloWorld()
	{
		System.out.println("Hello les gars!!! \n");
	}
	
	public FolderDao getFolderDao()
	{
		return this.folderDao;
	}
	
	
	//PARSING DE LA MORT QUI TUE
	public File toParse(int idFolder){
		
		Folder fold = new Folder();
		try {
			fold = folderDao.findById(idFolder);
		} 
		catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			
		///RECUPERE LA LISTE DES PHOTOS DU USER
	  	try {
	   			List<Document> listdoc = folderDao.listDocumentByFolder(idFolder);
	   			System.out.println(listdoc);
	  			
	  		} 
	  	catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	  		
	  	///RECUPERE LA LISTE DES ALBUMS DU USER
		try {
				List<Folder> listfolder = folderDao.findByIdUser(fold.getIdFolder());
				System.out.println(listfolder);
				}
		catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				}
		}
		
		////FICHIER XML CREE ET GENERE
		File xml = new File("/photos/"+fold.getUser().getLogin()+"gallery.xml");
		try {
			
			//open the stream
			  FileWriter fstream = new FileWriter("xml");
			  BufferedWriter out = new BufferedWriter(fstream);
			  
			  out.write("<?xml version=\"1.0\"?>");
			  out.write("<simpleviewergallery galleryStyle=\"MODERN\" title=\"Example de galerie\" textColor=\"FFFFFF\" frameColor=\"FFFFFF\" frameWidth=\"20\" thumbPosition=\"LEFT\" thumbColumns=\"3\" thumbRows=\"2\" showOpenButton=\"FALSE\" showFullscreenButton=\"TRUE\" maxImageWidth=\"800\" maxImageHeight=\"800\" useFlickr=\"false\" flickrUserName=\"\" flickrTags=\"\" languageCode=\"AUTO\" languageList=\"\" imagePath=\"images/\" thumbPath=\"thumbs/\" >");
			    
			    	for (Document it : (folderDao.listDocumentByFolder(idFolder))) {
			    	out.write("<image imageURL=\"");
			    	out.write(it.getPath());
			    	out.write("\" ");
			    	out.write("thumbURL=\"");
			    	out.write(it.getPath());
			    	out.write("\" ");
			    	out.write("linkURL=\"\" linkTarget=\"\"> ");
			    	out.write("</image>");
			    	}
			    	
			    out.write("</simpleviewergallery>");
			  
			    //Close the output stream
			  out.close();
			  }
		catch (Exception e){//Catch exception if any
			  System.err.println("Error: " + e.getMessage());
			  }

		
		return xml;
		
		
	}
	
	
	
	
	
	
	
}
